//
//  ViewController.m
//  photoviewer
//
//  Created by 楼楚 on 15/10/12.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "ViewController.h"
/* 用纯代码编写
 1.确定界面元素 内容
 2.用代码搭建界面
 3.编写代码
 */
@interface ViewController ()
@property (nonatomic,strong) UILabel *numLabel;
@property (nonatomic,strong) UIImageView *iconImage;
@property (nonatomic,strong) UILabel *descLabel;
@property (nonatomic,strong) UIButton *leftButton;
@property (nonatomic,strong) UIButton *rightButton;

@property (nonatomic,assign) int index;
@property (nonatomic,strong) NSArray *imageList;
@end

@implementation ViewController
/* 懒加载（延迟加载），通过getter实现
 效果：让对象在最需要的时候才创建
 */
 - (NSArray *)imageList
{
    NSLog(@"读取图像信息");
    if (_imageList == nil) {
        NSLog(@"实例化数组");
        NSDictionary *dict1 = @{@"name":@"facebook",@"desc":@"facebook"};
        NSDictionary *dict2 = @{@"name":@"google",@"desc":@"google"};
        NSDictionary *dict3 = @{@"name":@"twitter",@"desc":@"twitter"};
        NSDictionary *dict4 = @{@"name":@"youtube",@"desc":@"youtube"};
        NSDictionary *dict5 = @{@"name":@"LinkedIn",@"desc":@"LinkedIn"};
        _imageList = @[dict1,dict2,dict3,dict4,dict5];

    }
    return _imageList;
}
/*
   在viewDidLoad创建界面
 */
- (void)viewDidLoad {
    [super viewDidLoad];
// 1.  序号
    _numLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 40)];
//    _numLabel.text = @"1/5";
    _numLabel.textAlignment = NSTextAlignmentCenter;//文字居中
    
    [self.view addSubview:_numLabel];
    
//    2.图像
    CGFloat imageW = 200;
    CGFloat imageH = 200;
    CGFloat imageX = (self.view.bounds.size.width - imageW) * 0.5;
    CGFloat imageY = CGRectGetMaxY(self.numLabel.frame) + 20;
    
    _iconImage = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
    _iconImage.backgroundColor = [UIColor whiteColor];
//    _iconImage.image = [UIImage imageNamed:@"facebook"];
    [self.view addSubview:_iconImage];
    
//    3.描述文字
    
    CGFloat descY = CGRectGetMaxY(self.iconImage.frame);
    _descLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width, 100)];
//    _descLabel.text = @"facebook";
    _descLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_descLabel];
    
// 4.左边的按钮
    _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    CGFloat centerY = self.iconImage.center.y;
    CGFloat centerX = self.iconImage.frame.origin.x * 0.5;
    _leftButton.center = CGPointMake(centerX, centerY);
    
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
    [self.view addSubview:_leftButton];
    _leftButton.tag = -1;
    [_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
//    5.右边的按钮
    _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    _rightButton.center = CGPointMake(self.view.bounds.size.width - centerX,centerY);
    
    [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
    [self.view addSubview:_rightButton];
    _rightButton.tag = 1;
    [_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
//    NSDictionary *dict1 = @{@"name":@"facebook",@"desc":@"facebook"};
//        NSDictionary *dict2 = @{@"name":@"google",@"desc":@"google"};
//        NSDictionary *dict3 = @{@"name":@"twitter",@"desc":@"twitter"};
//        NSDictionary *dict4 = @{@"name":@"youtube",@"desc":@"youtube"};
//        NSDictionary *dict5 = @{@"name":@"LinkedIn",@"desc":@"LinkedIn"};
//    _imageList = @[dict1,dict2,dict3,dict4,dict5];

    [self showPhoto];
}
// 重构 ：相同的代码只出现一次
- (void) showPhoto
{
    self.numLabel.text = [NSString stringWithFormat:@"%d/%d",self.index + 1,5];
//    效率不高 每次都会生成数组
//    解决方法 使用属性记录字典数组
//    NSDictionary *dict1 = @{@"name":@"facebook",@"desc":@"facebook"};
//    NSDictionary *dict2 = @{@"name":@"google",@"desc":@"google"};
//    NSDictionary *dict3 = @{@"name":@"twitter",@"desc":@"twitter"};
//    NSDictionary *dict4 = @{@"name":@"youtube",@"desc":@"youtube"};
//    NSDictionary *dict5 = @{@"name":@"LinkedIn",@"desc":@"LinkedIn"};
//    NSArray *array = @[dict1,dict2,dict3,dict4,dict5];
    
    self.iconImage.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];
    self.descLabel.text = self.imageList[self.index][@"desc"];
    

    self.rightButton.enabled = (self.index != 4);
    self.leftButton.enabled = (self.index != 0);
}
- (void) clickButton:(UIButton *)button
{
    self.index += button.tag;
    [self showPhoto];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
    
    
    
}

@end
